package jin.pairprogram.jan18co11;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;



public class StuDao {
	public boolean StuRegister(Student stu) {
		BaseDao bd = new BaseDao();
		Connection con = bd.getCon();
		String sql1 = "insert into stu (name,stuid) values(?,?)";
		String sql2 = "insert into homework (name,stu_id,mark) values(?,?,?)";

		try {
			PreparedStatement ps1 = con.prepareStatement(sql1);
			PreparedStatement ps2 = con.prepareStatement(sql2);
			ps1.setString(1, stu.getName());
			ps1.setInt(2, stu.getStuid());
			ps1.executeUpdate();

			ps2.setString(1, stu.getName());
			ps2.setInt(2, stu.getStuid());
			ps2.setInt(3, stu.getMark());



			ps2.executeUpdate();

			if (ps1 != null) {
				ps1.close();
			}
			if (ps2 != null) {
				ps2.close();
			}
			if (con != null) {
				con.close();
			}

		} catch (SQLException e ) {
			e.printStackTrace();
		}
		return true;
	}

	
	 public boolean selectLast3(){
	    	BaseDao dao=new BaseDao();
	    	Connection con=dao.getCon();
	    	String sql="select * from stu as s right join homework h on s.name=h.name order by mark asc LIMIT 3";
	    	
	    	  try{		
	    	  PreparedStatement ps=con.prepareStatement(sql);
	    	  ResultSet rs=ps.executeQuery();
	    	  
	    	  ps.execute(); 
	    	  
	    	  while(rs.next()){
	    		  System.out.println(rs.getString("name")+" "+rs.getString("mark"));
	    	  }
	    	  if(rs!=null){
	    		  rs.close();
	    	  }
	    	  if(ps!=null){
	    		ps.close();    		   		
	    	  }
	    	  if(con!=null){
	    		con.close();    		   		
	    	  }		
	    			
	          }catch(SQLException e){
	    	     e.printStackTrace();
	    	}
	    	return true;
	    }


}
